//; ---------------------------------------------------------------------------
//; Object 27 - explosion	from a destroyed enemy or monitor
//; ---------------------------------------------------------------------------
#include "explosions.h"

#define id_Animals 0x28

#include "../sprite.h"
#include "../objectsystemNEWER.h"

static object *obj;

//; ---------------------------------------------------------------------------
//; Sprite mappings - explosion from a badnik or monitor
//; ---------------------------------------------------------------------------
byte mapexplode1[6] = { 1,
		     0xF8,   9,    0,    0, 0xF4
};
byte mapexplode2[6] = { 1,
		     0xF0, 0xF,    0,    6, 0xF0
};
byte mapexplode3[6] = { 1,
		     0xF0, 0xF,    0, 0x16, 0xF0
};
byte mapexplode4[21] = {4,
		     0xEC, 0xA,    0, 0x26, 0xEC,
		     0xEC,   5,    0, 0x2F,    4,
                        4,   5, 0x18, 0x2F, 0xEC,
		     0xFC, 0xA, 0x18, 0x26, 0xFC
};
byte mapexplode5[21] = {4,
		     0xEC, 0xA,    0, 0x33, 0xEC,
		     0xEC,   5,    0, 0x3C,    4,
		        4,   5, 0x18, 0x3C, 0xEC,
		     0xFC, 0xA, 0x18, 0x33, 0xFC
};

byte *Map_ExplodeItem[5] = { mapexplode1, mapexplode2, mapexplode3, mapexplode4, mapexplode5 };
//------

void (*ExItem_Index[5])() = { ExItem_Animal, ExItem_Main, ExItem_Animate };

//ExplosionItem:
void ExplosionItem(object *_obj)
{
    obj = _obj;

    //moveq	#0,d0
    //move.b	obRoutine(a0),d0
    byte routine = obj->Routine/2;

    //move.w	ExItem_Index(pc,d0.w),d1
    //jmp	ExItem_Index(pc,d1.w)
    (*ExItem_Index[routine])();
}

//ExItem_Animal:	; Routine 0
void ExItem_Animal()
{
    //addq.b	#2,obRoutine(a0)
    obj->Routine = obj->Routine + 2;

    //bsr.w	FindFreeObj
    object *animal = FindFreeObj();

    //bne.s	ExItem_Main
    if (animal->ID == 0)
    {
        //move.b	#id_Animals,0(a1) ; load animal object
        animal->ID = id_Animals;

	//move.w	obX(a0),obX(a1)
        animal->X = obj->X;

	//move.w	obY(a0),obY(a1)
        animal->Y = obj->Y;

	//move.w	$3E(a0),$3E(a1)
        ptrsetobjvariableword(0x3E, animal, ptrgetobjvariableword(0x3E, obj));
    }
    
    ExItem_Main();
}

//ExItem_Main:	; Routine 2
void ExItem_Main()
{
    //addq.b	#2,obRoutine(a0)
    obj->Routine = obj->Routine + 2;

    //move.l	#Map_ExplodeItem,obMap(a0)
    obj->Map = Map_ExplodeItem;

    //move.w	#$5A0,obGfx(a0)
    obj->Gfx = 0x5A0;

    //move.b	#4,obRender(a0)
    obj->Render = 4;

    //move.b	#1,obPriority(a0)
    obj->Priority = 1;

    //move.b	#0,obColType(a0)
    obj->ColType = 0;

    //move.b	#$C,obActWid(a0)
    obj->ActionWidth = 0xC;

    //move.b	#7,obTimeFrame(a0) ; set frame duration to 7 frames
    obj->TimeFrame = 7;

    //move.b	#0,obFrame(a0)
    obj->Frame = 0;

    //sfx	sfx_BreakItem	; play breaking enemy sound
   
    ExItem_Animate();
}

//ExItem_Animate:	; Routine 4 (2 for ExplosionBomb)
void ExItem_Animate()
{
    //subq.b	#1,obTimeFrame(a0) ; subtract 1 from frame duration
    obj->TimeFrame = obj->TimeFrame - 1;

    //bpl.s	@display
    if (obj->TimeFrame >= 0)
        DisplaySprite(obj);
    else
    {
        //move.b	#7,obTimeFrame(a0) ; set frame duration to 7 frames
	obj->TimeFrame = 7;

       	//addq.b	#1,obFrame(a0)	; next frame
        obj->Frame = obj->Frame + 1;

	//cmpi.b	#5,obFrame(a0)	; is the final frame (05) displayed?
	if (obj->Frame == 5)
      	{
            //beq.w	DeleteObject	; if yes, branch
            DeleteObject(obj);
        }
        else
        {
	    //@display:
	    //bra.w	DisplaySprite
            DisplaySprite(obj);
        }
    }
}

